package yxy.game.pm2.command.camptower;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.Constant;
import yxy.game.pm2.bean.module.rank.RankEntry;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.CampTowerMessage;
import yxy.game.pm2.module.camptower.RankQunCampTower;
import yxy.game.pm2.module.camptower.RankShuCampTower;
import yxy.game.pm2.module.camptower.RankWeiCampTower;
import yxy.game.pm2.module.camptower.RankWuCampTower;
import yxy.game.pm2.module.player.PlayerHandle;

import java.util.Collection;

/**
 * 阵营塔排行榜请求
 *
 * @ClassName CMD_CampTowerRank
 * @Author jingtao-zheng
 * @Date 2022/8/2 16:10
 * @Version 1.0
 **/
@MessageType(proto = Command.CampTowerRank, classtype = CampTowerMessage.CampTowerRankRequest.class)
public class CMD_CampTowerRank extends Command<CampTowerMessage.CampTowerRankRequest> {
    @Override
    public void execute(MessageSource messageSource, CampTowerMessage.CampTowerRankRequest request) throws Exception {
        String playerCode = messageSource.getTag();
        //获取阵营类型
        int type = request.getType().getNumber();

        CampTowerMessage.CampTowerRankResponse.Builder response = CampTowerMessage.CampTowerRankResponse.newBuilder();
        if(type == Constant.WEI){
            extractedWei(messageSource, playerCode,  response);
        }
        if(type == Constant.SHU){
            extractedShu(messageSource, playerCode, response);
        }
        if(type == Constant.WU){
            extractedWu(messageSource, playerCode,response);
        }
        if(type == Constant.QUN){
            extractedQun(messageSource, playerCode, response);
        }


    }

    private void extractedWei(MessageSource messageSource, String playerCode, CampTowerMessage.CampTowerRankResponse.Builder response) {
        RankWeiCampTower rankTower = RankWeiCampTower.getRank(playerCode);

        try {
            PlayerHandle playerHandle = new PlayerHandle();
            Collection<RankEntry> top = rankTower.top(rankTower.topSize());
            int rank = 0;
            for (RankEntry entry : top) {
                String pCode = entry.getKey();
                int floor = entry.getValue().intValue();

                CampTowerMessage.RankTower.Builder builder = CampTowerMessage.RankTower.newBuilder();
                builder.setRank(++rank);
                builder.setPlayerInfo(playerHandle.playerSimpleInfo(pCode));
                builder.setFloor(floor);
                response.addRank(builder);
            }

            {
                int playerRank = 0;
                int floor = 0;
                RankEntry entry = rankTower.get(playerCode);
                if (entry != null) {
                    playerRank = rankTower.rank(playerCode);
                    floor = entry.getValue().intValue();
                }
                CampTowerMessage.RankTower.Builder builder = CampTowerMessage.RankTower.newBuilder();
                builder.setRank(playerRank);
                builder.setPlayerInfo(playerHandle.playerSimpleInfo(playerCode));
                builder.setFloor(floor);
                response.setMy(builder);
            }


            send(messageSource, response, Result.Success);
        } catch (Exception e) {
            e.printStackTrace();
            send(messageSource, response, Result.DataError);
        }
    }

    private void extractedShu(MessageSource messageSource, String playerCode, CampTowerMessage.CampTowerRankResponse.Builder response) {
        RankShuCampTower rankTower = RankShuCampTower.getRank(playerCode);

        try {
            PlayerHandle playerHandle = new PlayerHandle();
            Collection<RankEntry> top = rankTower.top(rankTower.topSize());
            int rank = 0;
            for (RankEntry entry : top) {
                String pCode = entry.getKey();
                int floor = entry.getValue().intValue();

                CampTowerMessage.RankTower.Builder builder = CampTowerMessage.RankTower.newBuilder();
                builder.setRank(++rank);
                builder.setPlayerInfo(playerHandle.playerSimpleInfo(pCode));
                builder.setFloor(floor);
                response.addRank(builder);
            }

            {
                int playerRank = 0;
                int floor = 0;
                RankEntry entry = rankTower.get(playerCode);
                if (entry != null) {
                    playerRank = rankTower.rank(playerCode);
                    floor = entry.getValue().intValue();
                }
                CampTowerMessage.RankTower.Builder builder = CampTowerMessage.RankTower.newBuilder();
                builder.setRank(playerRank);
                builder.setPlayerInfo(playerHandle.playerSimpleInfo(playerCode));
                builder.setFloor(floor);
                response.setMy(builder);
            }


            send(messageSource, response, Result.Success);
        } catch (Exception e) {
            e.printStackTrace();
            send(messageSource, response, Result.DataError);
        }
    }

    private void extractedWu(MessageSource messageSource, String playerCode, CampTowerMessage.CampTowerRankResponse.Builder response) {
        RankWuCampTower rankTower = RankWuCampTower.getRank(playerCode);

        try {
            PlayerHandle playerHandle = new PlayerHandle();
            Collection<RankEntry> top = rankTower.top(rankTower.topSize());
            int rank = 0;
            for (RankEntry entry : top) {
                String pCode = entry.getKey();
                int floor = entry.getValue().intValue();

                CampTowerMessage.RankTower.Builder builder = CampTowerMessage.RankTower.newBuilder();
                builder.setRank(++rank);
                builder.setPlayerInfo(playerHandle.playerSimpleInfo(pCode));
                builder.setFloor(floor);
                response.addRank(builder);
            }

            {
                int playerRank = 0;
                int floor = 0;
                RankEntry entry = rankTower.get(playerCode);
                if (entry != null) {
                    playerRank = rankTower.rank(playerCode);
                    floor = entry.getValue().intValue();
                }
                CampTowerMessage.RankTower.Builder builder = CampTowerMessage.RankTower.newBuilder();
                builder.setRank(playerRank);
                builder.setPlayerInfo(playerHandle.playerSimpleInfo(playerCode));
                builder.setFloor(floor);
                response.setMy(builder);
            }


            send(messageSource, response, Result.Success);
        } catch (Exception e) {
            e.printStackTrace();
            send(messageSource, response, Result.DataError);
        }
    }

    private void extractedQun(MessageSource messageSource, String playerCode, CampTowerMessage.CampTowerRankResponse.Builder response) {
        RankQunCampTower rankTower = RankQunCampTower.getRank(playerCode);

        try {
            PlayerHandle playerHandle = new PlayerHandle();
            Collection<RankEntry> top = rankTower.top(rankTower.topSize());
            int rank = 0;
            for (RankEntry entry : top) {
                String pCode = entry.getKey();
                int floor = entry.getValue().intValue();

                CampTowerMessage.RankTower.Builder builder = CampTowerMessage.RankTower.newBuilder();
                builder.setRank(++rank);
                builder.setPlayerInfo(playerHandle.playerSimpleInfo(pCode));
                builder.setFloor(floor);
                response.addRank(builder);
            }

            {
                int playerRank = 0;
                int floor = 0;
                RankEntry entry = rankTower.get(playerCode);
                if (entry != null) {
                    playerRank = rankTower.rank(playerCode);
                    floor = entry.getValue().intValue();
                }
                CampTowerMessage.RankTower.Builder builder = CampTowerMessage.RankTower.newBuilder();
                builder.setRank(playerRank);
                builder.setPlayerInfo(playerHandle.playerSimpleInfo(playerCode));
                builder.setFloor(floor);
                response.setMy(builder);
            }


            send(messageSource, response, Result.Success);
        } catch (Exception e) {
            e.printStackTrace();
            send(messageSource, response, Result.DataError);
        }
    }

    private void send(MessageSource messageSource, CampTowerMessage.CampTowerRankResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }
}
